﻿using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine.EventSystems;

public class LabelPos
{
    public const int L = 0x1;
    public const int R = 0x2;
    public const int HC = 0x4;
    public const int T = 0x8;
    public const int B = 0x10;
    public const int VC = 0x20;
}

public static class GuiUtil
{
    static GUIContent content = new GUIContent();
//    public class GUILabelCache
//    {
//        public Rect position;
//        public string str;
//        public GUIStyle style;
//        public GUILabelCache(Rect position, string str, GUIStyle style)
//        {
//            this.position = position;
//            this.str = str;
//            this.style = style;
//        }
//        public void draw()
//        {
//            GUI.Label(position, str, style);
//        }
//    }

    public static void Label(string str, int fontSize, int posType, float width = 0, float height = 0)
    {
        int lineCount = 1;
        int i = 0;
        while(i < str.Length)
        {
            i = str.IndexOf('\n', i);
            if(i < 0)
            {
                break;
            }
            lineCount++;
            i++;
        }

        GUIStyle style = new GUIStyle(GUI.skin.textField);
        //      if(Application.isEditor)
        //      {
        //          fontSize = (int)(fontSize * 0.5f);
        //      }
        //      else if(Application.platform == RuntimePlatform.IPhonePlayer)
        //      {
        //          fontSize = (int)(fontSize * 0.6f);
        //      }

        fontSize = (int)Math.Round(Mathf.Max((float)Screen.width / 1080, (float)Screen.height / 1920) * fontSize);

        style.fontSize = fontSize;
        Vector2 size = new Vector2();
        size.x = width;
        size.y = height;
        if(width == 0 || height == 0)
        {
            content.text = str;
            size = style.CalcSize(content);
            if(width != 0)
            {
                size.x = width;
            }

            if(height != 0)
            {
                size.y = height;
            }
        }

        int screenWidth = Screen.width;
        int screenHeight = Screen.height;
        if (!Application.isEditor)
        {
            if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown)
            {
                if (screenWidth > screenHeight)
                {
                    int s = screenWidth;
                    screenWidth = screenHeight;
                    screenHeight = s;
                }
            }
            else
            {
                if (screenWidth < screenHeight)
                {
                    int s = screenWidth;
                    screenWidth = screenHeight;
                    screenHeight = s;
                }
            }
        }

        float left = 0;
        if((posType & LabelPos.R) != 0)
        {
            left = screenWidth - size.x;
        }
        else if((posType & LabelPos.HC) != 0)
        {
            left = (screenWidth - size.x) / 2;
        }


        float top = 0;
        if((posType & LabelPos.B) != 0)
        {
            top = screenHeight - size.y;
        }
        else if((posType & LabelPos.VC) != 0)
        {
            top = (screenHeight - size.y) / 2;
        }

        Rect position = new Rect(left, top, size.x, size.y);
//        GUILabelCache cache = new GUILabelCache(position, str, style);
        GUI.Label(position, str, style);    
//        return cache;
    }
    public static Rect getLabelSize(string str, int fontSize, int posType, float width = 0, float height = 0)
    {
        int lineCount = 1;
        int i = 0;
        while(i < str.Length)
        {
            i = str.IndexOf('\n', i);
            if(i < 0)
            {
                break;
            }
            lineCount++;
            i++;
        }

        GUIStyle style = new GUIStyle(GUI.skin.textField);
        //      if(Application.isEditor)
        //      {
        //          fontSize = (int)(fontSize * 0.5f);
        //      }
        //      else if(Application.platform == RuntimePlatform.IPhonePlayer)
        //      {
        //          fontSize = (int)(fontSize * 0.6f);
        //      }

        fontSize = (int)Math.Round(Mathf.Max((float)Screen.width / 1080, (float)Screen.height / 1920) * fontSize);

        style.fontSize = fontSize;
        Vector2 size = new Vector2();
        size.x = width;
        size.y = height;
        if(width == 0 || height == 0)
        {
            content.text = str;
            size = style.CalcSize(content);
            if(width != 0)
            {
                size.x = width;
            }

            if(height != 0)
            {
                size.y = height;
            }
        }

        int screenWidth = Screen.width;
        int screenHeight = Screen.height;
        if (!Application.isEditor)
        {
            if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown)
            {
                if (screenWidth > screenHeight)
                {
                    int s = screenWidth;
                    screenWidth = screenHeight;
                    screenHeight = s;
                }
            }
            else
            {
                if (screenWidth < screenHeight)
                {
                    int s = screenWidth;
                    screenWidth = screenHeight;
                    screenHeight = s;
                }
            }
        }

        float left = 0;
        if((posType & LabelPos.R) != 0)
        {
            left = screenWidth - size.x;
        }
        else if((posType & LabelPos.HC) != 0)
        {
            left = (screenWidth - size.x) / 2;
        }


        float top = 0;
        if((posType & LabelPos.B) != 0)
        {
            top = screenHeight - size.y;
        }
        else if((posType & LabelPos.VC) != 0)
        {
            top = (screenHeight - size.y) / 2;
        }

        Rect position = new Rect(left, top, size.x, size.y);
        return position;
//        GUILabelCache cache = new GUILabelCache(position, str, style);
//        GUI.Label(position, str, style);    
//        return cache;
    }
}